package com.jalarbee.core.generic.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jalarbee.core.generic.dao.GenericDao;
import com.jalarbee.core.generic.service.GenericService;
import com.jalarbee.core.util.Chunk;

@Transactional(readOnly = true)
public abstract class GenericServiceImpl<T, PK extends Serializable> implements GenericService<T, PK> {

	protected GenericDao<T, PK> dao;

	protected abstract void setDao(GenericDao<T, PK> dao);
	
	protected static final Logger logger = Logger.getLogger(GenericServiceImpl.class);
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public T create(T newInstance) {
		dao.create(newInstance);
		return newInstance;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(T instance) {
		dao.delete(instance);
	}

	public T read(PK key) {
		return dao.read(key);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void update(T instance) {
		dao.update(instance);
	}

	public List<T> findAll(Chunk chunk) {
		return dao.findAll(chunk);
	}

}
